import Variable from "../constants/index"
import { ResponseData } from "../types/common"
import { HttpMethod } from "../types/enum"

export default class RequestHttp {
  static get<T>(url: string, params?: object) {
    return requset<T>(url, HttpMethod.GET, params)
  }
  static post() { }
  static put() { }
  static delete() { }
}

function requset<T>(url: string, method: HttpMethod, data?: object) {
  return new Promise<T>((resolve, reject) => {
    wx.request<ResponseData<T>>({
      url: Variable.BASE_URL + url,
      method,
      data,
      success(res) {
        // console.log(res.data);
        
        if (res.data.code === 200) {
          resolve(res.data.data)
        }else{
          wx.showToast({
            icon:'error',
            title:res.data.msg
          })
          reject(new Error(res.data.msg))
        }
      },
      fail(res) {
        reject(new Error(res.errMsg))
      }
    })
  })
}